﻿using LivestreamE_Commerce.Dto.Tree;
using LivestreamE_Commerce.EntityFrameworkCore;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LivestreamE_Commerce.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MyOrganizationController : ControllerBase
    {
        LivestreamE_CommerceDbContext db;
        public MyOrganizationController(LivestreamE_CommerceDbContext _db)
        {
            db = _db;
        }
        [HttpGet, Route("OrganizationTree")]
        public IActionResult OrganizationTree()
        {
            var list = OrganizationTrees("");
            return Ok(new { list = list, state = true });
        }
        private List<OrganizationTreeDto> OrganizationTrees(string FenId)
        {
            var list = db.OrganizationModel.ToList();
            var newlist = list.Where(x => Convert.ToString(x.FatherId).Equals(FenId)).ToList();
            List<OrganizationTreeDto> result = new List<OrganizationTreeDto>();
            newlist.ForEach(p =>
            {
                var t = new OrganizationTreeDto();
                t.Id = p.Id.ToString();
                t.Label = p.OrganizationName;
                t.Url = p.Url;
                t.Children = OrganizationTrees(p.Id.ToString());
                result.Add(t);
            });
            return result;
        }

    }
}
